package org.vestbjergbyggecenter.Model;

import java.util.ArrayList;
import java.util.Iterator;

public class ProductRecords {
	private ArrayList<Product> products;
	private static ProductRecords instance = null;
	
	private ProductRecords()
	{
		products = new ArrayList<Product>();
	}
	
	public static ProductRecords getInstance()
    {
        if(instance == null)
        {
            instance = new ProductRecords();
        }
        
        return instance;
    }

	public ArrayList<Product> getProduct() {
		return products;
	}

	public void setProduct(ArrayList<Product> products) {
		this.products = products;
	}	
	
	public void addProduct(Product newProduct)
	{		
		products.add(newProduct);
	}
	
	public Product getProduct(int id)
	{
		Product theProduct = null;
        boolean found = false;
        Iterator<Product> it = products.iterator();
        while(it.hasNext() && !found)
        {
            Product product = it.next();
            if(product.getId() == id)
            {
                theProduct = product;
                found = true;
            }
        }        
        return theProduct;		
	}
	
	public void updateProduct(int id, String name, Category category, double price,int quantity,String location )
	{
		Product product = getProduct(id);
		product.setName(name);
		product.setCategory(category);
		product.setOriginalPrice(price);
		product.setQuantity(quantity);
		product.setLocation(location);
		
	}
	
	public void removeProduct(int id)
	{
		if(getProduct(id)==null)
		products.remove(getProduct(id));		
	}
	
	public void print()
	{
		System.out.println("List of the products!");
		for(Product product:products)
		{
			product.print();
			System.out.println();
		}
	}
}
